package com.zhongkai.web.control.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhongkai.service.config.ButtonService;
import com.zhongkai.service.config.MenuService;
import com.zhongkai.tools.CheckWord;


@Controller
@RequestMapping(value="/system/ajax/menu.do")
public class MenuAjaxControl {
	
	private Logger log=Logger.getLogger(this.getClass());
	private MenuService menuService;
	private ButtonService buttonService;
	
	
	@RequestMapping(params="method=validate")
	public void validate(HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			String actionKey=request.getParameter("actionKey");
			if(actionKey!=null&&!"".equals(actionKey)){
				boolean isExistActionKey=menuService.isExistActionKey(actionKey);
				if(!isExistActionKey){
					isExistActionKey=buttonService.isExistActionKey(actionKey);
				}
				if(!isExistActionKey){
					toJsp(response, "<span style='color: green'>[可以注册该actionKey]</span>");
				}
				else toJsp(response,"<span style='color: red'>[该菜单的actionKey已存在]</span>");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("删除时发生异常，请与管理员联系!", "/system/config/menulist.jsp"));
		}
	}
	
	public void toJsp(HttpServletResponse response, String toJspData) throws IOException {
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");
			PrintWriter out = response.getWriter();
			out.println(toJspData);		
			out.flush();
			out.close();
	}

	public MenuService getMenuService() {
		return menuService;
	}

	@Resource
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public ButtonService getButtonService() {
		return buttonService;
	}

	@Resource
	public void setButtonService(ButtonService buttonService) {
		this.buttonService = buttonService;
	}
	
	
	

}
